package com.FH.organization.emailRevice;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

public class MailSendUtil {

    public static void sendHtmlMail(MailInfo info)throws Exception{

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);

        final Properties p = System.getProperties() ;
        p.setProperty("mail.smtp.host", info.getHost());
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.user", info.getFormName());
        p.setProperty("mail.smtp.pass", info.getFormPassword());

        if(info.getFormName().contains("qq.com")){
            p.put("mail.smtp.ssl.enable", "true");
            p.put("mail.smtp.ssl.socketFactory", sf);
        }

        Session session = Session.getInstance(p, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
            }
        });
        session.setDebug(true);

        Message message = new MimeMessage(session);

        //消息发送的主题
        message.setSubject(info.getSubject());
        //接受消息的人
//        message.setReplyTo(info.getReplayAddress());
//        message.setReplyTo(info.getReplayAddress());
        //消息的发送者
        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),info.getUserName()));

        InternetAddress[] sendTo = new InternetAddress[info.getToAddress().size()];
        for (int i = 0; i < info.getToAddress().size(); i++) {
            sendTo[i] = new InternetAddress(info.getToAddress().get(i));
        }
        // 创建邮件的接收者地址，并设置到邮件消息中
        message.setRecipients(Message.RecipientType.TO,sendTo);

        if(info.getReplayAddress().size() > 0){
            InternetAddress[] copyTo = new InternetAddress[info.getReplayAddress().size()];
            for (int i = 0; i < info.getReplayAddress().size(); i++) {
                copyTo[i] = new InternetAddress(info.getReplayAddress().get(i));
            }
            //设置邮件抄送人
            message.setRecipients(Message.RecipientType.CC,copyTo);
        }

        // 消息发送的时间
        message.setSentDate(new Date());

        // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(info.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);

        if(null != info.getAttechment()){
            BodyPart attachmentBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(info.getAttechment());
            attachmentBodyPart.setDataHandler(new DataHandler(source));
            attachmentBodyPart.setFileName(info.getAttechment().getName());
            mainPart.addBodyPart(attachmentBodyPart);
        }


        // 将MiniMultipart对象设置为邮件内容
        message.setContent(mainPart);

        Transport transport = session.getTransport("smtp");
        transport.connect((String)p.get("mail.smtp.host"), (String)p.get("username"), (String)p.get("password"));
        transport.sendMessage(message,message.getAllRecipients());
        transport.close();
//        Transport.send(message);

    }
    
//    private static Message getMessage(MailInfo info) throws Exception{
//
//        MailSSLSocketFactory sf = new MailSSLSocketFactory();
//        sf.setTrustAllHosts(true);
//
//        final Properties p = System.getProperties() ;
//        p.setProperty("mail.smtp.host", info.getHost());
//        p.setProperty("mail.smtp.auth", "true");
//        p.setProperty("mail.smtp.user", info.getFormName());
//        p.setProperty("mail.smtp.pass", info.getFormPassword());
//
//        if(info.getFormName().contains("qq.com")){
//            p.put("mail.smtp.ssl.enable", "true");
//            p.put("mail.smtp.ssl.socketFactory", sf);
//        }
//
//        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
//        Session session = Session.getInstance(p, new Authenticator(){
//            protected PasswordAuthentication getPasswordAuthentication() {
//                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
//            }
//        });
//        session.setDebug(true);
//        Message message = new MimeMessage(session);
//        //消息发送的主题
//        message.setSubject(info.getSubject());
//        //接受消息的人
////        message.setReplyTo(info.getReplayAddress());
////        message.setReplyTo(info.getReplayAddress());
//        //消息的发送者
//        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),info.getUserName()));
//
//        InternetAddress[] sendTo = new InternetAddress[info.getToAddress().size()];
//        for (int i = 0; i < info.getToAddress().size(); i++) {
//            sendTo[i] = new InternetAddress(info.getToAddress().get(i));
//        }
//        // 创建邮件的接收者地址，并设置到邮件消息中
//        message.setRecipients(Message.RecipientType.TO,sendTo);
//
//        if(info.getReplayAddress().size() > 0){
//            InternetAddress[] copyTo = new InternetAddress[info.getReplayAddress().size()];
//            for (int i = 0; i < info.getReplayAddress().size(); i++) {
//                copyTo[i] = new InternetAddress(info.getReplayAddress().get(i));
//            }
//            //设置邮件抄送人
//            message.setRecipients(Message.RecipientType.CC,copyTo);
//        }
//
//        // 消息发送的时间
//        message.setSentDate(new Date());
//
//        return message ;
//    }
    
    
    
//    public static void main(String[] args) throws Exception{
//        String mail = "jacksonjinwz@163.com"; //发送对象的邮箱
//        String title = "我有一句话跟你说";
//        String content = "<div>你不在学校吗？</div><br/><hr/><div>记得28号来学校</div>";
//
//        InternetAddress[] sr = InternetAddress.parse("jacksonjinwz@163.com");
//
//        MailInfo info = new MailInfo();
//        info.setToAddress(mail);
//        info.setSubject(title);
//        info.setContent(content);
//        info.setHost("smtp.163.com");
//        info.setFormName("yan12319@163.com");
//        info.setFormPassword("yan5790054");
//        info.setReplayAddress(sr);
//        info.setUserName("jinwz");
//        try {
//            MailSendUtil.sendHtmlMail(info);
//        } catch (Exception e) {
//            System.out.print("'"+title+"'的邮件发送失败！");
//            e.printStackTrace();
//        }
//
//    }
    
}
